package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.ZhiCategory;
import com.kede.app.po.ModifyCardGroupPo;
import com.kede.app.service.ZhiCategoryService;
import com.kede.app.service.dto.ZhiCategoryQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author Pr.li
* @date 2023-07-02
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "zhiCategory管理")
@RequestMapping("/api/zhiCategory")
public class ZhiCategoryController {

    private final ZhiCategoryService zhiCategoryService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('zhiCategory:list')")
    public void exportZhiCategory(HttpServletResponse response, ZhiCategoryQueryCriteria criteria) throws IOException {
        zhiCategoryService.download(zhiCategoryService.queryAll(criteria), response);
    }

    /**
    * 查询zhiCategory
    **/
    @GetMapping
    @Log("查询zhiCategory")
    @ApiOperation("查询zhiCategory")
    @PreAuthorize("@el.check('zhiCategory:list')")
    public ResponseEntity<Object> queryZhiCategory(ZhiCategoryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(zhiCategoryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增zhiCategory
    **/
    @PostMapping
    @Log("新增zhiCategory")
    @ApiOperation("新增zhiCategory")
    @PreAuthorize("@el.check('zhiCategory:add')")
    public ResponseEntity<Object> createZhiCategory(@Validated @RequestBody ZhiCategory resources){
        return new ResponseEntity<>(zhiCategoryService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改zhiCategory
    **/
    @PutMapping
    @Log("修改zhiCategory")
    @ApiOperation("修改zhiCategory")
    @PreAuthorize("@el.check('zhiCategory:edit')")
    public ResponseEntity<Object> updateZhiCategory(@Validated @RequestBody ZhiCategory resources){
        zhiCategoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除zhiCategory
    **/
    @DeleteMapping
    @Log("删除zhiCategory")
    @ApiOperation("删除zhiCategory")
    @PreAuthorize("@el.check('zhiCategory:del')")
    public ResponseEntity<Object> deleteZhiCategory(@RequestBody Integer[] ids) {
        zhiCategoryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("getChildsById")
    @ApiOperation("获取下级子分类")
    @PreAuthorize("@el.check('getChildsById')")
    public ResponseEntity<ZhiCategory> fillListChildsById(Integer id){
        return new ResponseEntity(zhiCategoryService.getChildsById(id),HttpStatus.OK);
    }

    @ApiOperation("获取分类作用信息")
    @GetMapping("getOneZhiCateInfoById")
    public ResponseEntity<ZhiCategory> getOneZhiCateInfoById(Integer id){
        return new ResponseEntity(zhiCategoryService.getOneZhiCateInfoById(id),HttpStatus.OK);
    }

    @ApiOperation("修改类型名称")
    @PostMapping("modifyCardGroup")
    public ResponseEntity modifyCardGroup(@RequestBody ModifyCardGroupPo modifyCardGroupPo){
        return new ResponseEntity(zhiCategoryService.modifyCardGroup(modifyCardGroupPo),HttpStatus.OK);
    }

    @DeleteMapping("delCard")
    public ResponseEntity delCard(@RequestBody Integer[] ids){
        return new ResponseEntity(zhiCategoryService.delCard(ids),HttpStatus.OK);
    }

}